package echat.future.cn.echat.home

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.RadioGroup
import com.zhusx.core.utils._Views
import echat.future.cn.echat.R

/**
 *
 * Author  zhusx
 * Email   327270607@qq.com
 * Create  2018/8/9 20:37
 */
class ChoosePopup(context: Context, type: Int, listener: OnItemCheckedListener) : PopupWindow(context) {
    var xoff: Float = 0f

    init {
        when (type) {
            0 -> {
                xoff = 60f
                contentView = LayoutInflater.from(context).inflate(R.layout.popup_choose_xj, null, false)
                contentView.findViewById<View>(R.id.radio_1).setOnClickListener {
                    listener.onChecked(type, 0)
                    this@ChoosePopup.dismiss()
                }
                contentView.findViewById<View>(R.id.radio_2).setOnClickListener {
                    listener.onChecked(type, 1)
                    this@ChoosePopup.dismiss()
                }
                contentView.findViewById<View>(R.id.radio_3).setOnClickListener {
                    listener.onChecked(type, 2)
                    this@ChoosePopup.dismiss()
                }
            }
            1 -> {
                xoff = 25f
                contentView = LayoutInflater.from(context).inflate(R.layout.popup_choose_sex, null, false)
                contentView.findViewById<View>(R.id.radio_1).setOnClickListener {
                    listener.onChecked(type, 0)
                    this@ChoosePopup.dismiss()
                }
                contentView.findViewById<View>(R.id.radio_2).setOnClickListener {
                    listener.onChecked(type, 1)
                    this@ChoosePopup.dismiss()
                }
            }
        }
        setBackgroundDrawable(ColorDrawable())
        width = ViewGroup.LayoutParams.WRAP_CONTENT
        height = ViewGroup.LayoutParams.WRAP_CONTENT
        isOutsideTouchable = true

    }

    override fun showAsDropDown(anchor: View?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            super.showAsDropDown(anchor, -_Views.dip2px(xoff), _Views.dip2px(15f), Gravity.CENTER_HORIZONTAL)
        } else {
            super.showAsDropDown(anchor, -_Views.dip2px(xoff), _Views.dip2px(15f))
        }
    }

    interface OnItemCheckedListener {
        fun onChecked(type: Int, index: Int)
    }
}